#include "../../../head.h"

int main(int argc, char const *argv[])
{
    // 安全防护
    if (argc < 2)
    {
        printf("格式错误! ./%s file", argv[0]);
        return 0;
    }

    // 创建文件指针数组
    FILE *fp[argc-1];
    for (size_t i = 0; i < argc - 1; i++)
    {
        fp[i] = fopen(argv[i+1] , "r+");
    }
    FILE *fp_all = fopen("all_file" , "a+");

    char buf[argc-1][1024];

    fseek(fp_all , 0 , SEEK_SET);
    // 合并文件
    for (size_t i = 0; i < argc - 1; i++)
    {
        fseek(fp[i] , 0 , SEEK_SET);
        int byte = 0;
        while ((byte = fread(buf[i] , 1 , sizeof(buf[i]) , fp[i])) > 0)
        {
            fwrite(buf[i] , 1 , byte , fp_all);
        }
    }
    
    // 关闭文件
    for (size_t i = 0; i < argc - 1; i++)
    {
        fclose(fp[i]);
    }
    fclose(fp_all);
    
    
    return 0;
}
